﻿// ----------------------------------------------------------------------------
//  Copyright (C) 成都联宇创新科技有限公司 版权所有。 
//      
//  文件名：CryptographyModule.cs
//  文件功能描述：
//  创建标识：骆智慧 2016/03/02 23:28
//     
//  修改标识：骆智慧  2016/03/10/06:40
//  修改描述：
//  ----------------------------------------------------------------------------

#region 导入名称空间

using System.Diagnostics;
using System.Text;
using Autofac;

#endregion

namespace LightWork.Cryptography
{
    /// <summary>
    /// 加密/解密模块
    /// </summary>
    public class CryptographyModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            using (var pwdFs = ThisAssembly.GetManifestResourceStream("Foundation.Cryptography.EncryptFilePwd"))
            {
                var defaultEncoding = Encoding.UTF8;

                Debug.Assert(pwdFs != null, "默认密码不能为NULL");
                var pwdhash = CryptoUtility.GetHashed(pwdFs, HashAlgorithmEnum.SHA256);

                using (var saltFs = ThisAssembly.GetManifestResourceStream("Foundation.Cryptography.EncryptFileSalt"))
                {
                    Debug.Assert(saltFs != null, "默认盐值不能为NULL");
                    var salthash = CryptoUtility.GetHashed(saltFs, HashAlgorithmEnum.SHA1);

                    builder.RegisterType<DataEncryptionStandard>().AsSelf()
                        .WithProperty("DefaultPassword", defaultEncoding.GetBytes(pwdhash))
                        .WithProperty("DefaultEncoding", defaultEncoding)
                        .WithProperty("DefaultSalt", defaultEncoding.GetBytes(salthash))
                        .As<ICryptoService>();
                    builder.RegisterType<AdvancedEncryptionStandard>().AsSelf()
                        .WithProperty("DefaultPassword", defaultEncoding.GetBytes(pwdhash))
                        .WithProperty("DefaultEncoding", defaultEncoding)
                        .WithProperty("DefaultSalt", defaultEncoding.GetBytes(salthash))
                        .As<ICryptoService>();
                }
            }
        }
    }
}